package chencb.stu.thrift.config;

import chencb.stu.thrift.impl.UserInterImpl;
import chencb.stu.thrift.interfaces.IUserInter;
import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;


/**
 * @Author: chenchunbao
 * @Date: 18-7-16 下午7:12
 * @Description:服务配置及接口注册
 */

@Component
public class ThriftServerConfig implements InitializingBean {
    private Logger logger = LoggerFactory.getLogger(ThriftServerConfig.class);
    @Value("${thrift.server.port}")
    private int port;
    @Value("${thrift.server.minWorkerThreads}")
    private int minThreads;
    @Value("${thrift.server.maxWorkerThreads}")
    private int maxThreads;
    @Autowired
    private UserInterImpl userInterImpl;

    @Override
    public void afterPropertiesSet() {
        logger.info("=============启动thrift 服务注册=======");
        TMultiplexedProcessor tMultiplexedProcessor = new TMultiplexedProcessor();
        // setOut(tMultiplexedProcessor);
        setProcessor(tMultiplexedProcessor);
        ThriftServerThread thriftServerThread = new ThriftServerThread(tMultiplexedProcessor);
        thriftServerThread.start();
    }

    class ThriftServerThread extends Thread {
        private TThreadPoolServer server;

        ThriftServerThread(TMultiplexedProcessor tMultiplexedProcessor) {
            try {
                TServerTransport serverTransport = new TServerSocket(port);
                TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport);
                tArgs.processor(tMultiplexedProcessor);
                tArgs.transportFactory(new TFramedTransport.Factory());
                tArgs.protocolFactory(new TBinaryProtocol.Factory(true, true));
                tArgs.minWorkerThreads(minThreads);
                tArgs.maxWorkerThreads(maxThreads);
                server = new TThreadPoolServer(tArgs);
            } catch (Exception e) {
                logger.error("thrift服务端启动出错", e);
            }
        }

        @Override
        public void run() {
            //启动服务
            server.serve();
        }
    }


    /**
     * 对内服务接口注册
     *
     * @param tMultiplexedProcessor
     */

    private void setProcessor(TMultiplexedProcessor tMultiplexedProcessor) {
        IUserInter.Processor datapoolServer = new IUserInter.Processor<IUserInter.Iface>(userInterImpl);
        tMultiplexedProcessor.registerProcessor(IUserInter.class.getName(), datapoolServer);
    }
}

